package cn.gailvlun.gll.presentation.chat;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.app.SharedElementCallback;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.gailvlun.gll.R;
import cn.gailvlun.gll.databinding.ActivityPhotoListBinding;
import cn.gailvlun.gll.presentation.base.BaseActivity;
import cn.gailvlun.gll.presentation.chat.adapter.PhotoListPagerAdapter;



public class PhotoListActivity extends BaseActivity {

    public static void openActivity(Context context, ArrayList<String> photos, int pos) {
        Intent intent = new Intent(context, PhotoListActivity.class);
        intent.putStringArrayListExtra("photos", photos);
        intent.putExtra("position", pos);
        context.startActivity(intent);
    }

    public static void openActivity(Context context, ArrayList<String> photos, int pos, @Nullable Bundle transBundle) {
        Intent intent = new Intent(context, PhotoListActivity.class);
        intent.putStringArrayListExtra("photos", photos);
        intent.putExtra("position", pos);
        context.startActivity(intent, transBundle);
    }

    private final SharedElementCallback mSharedElementCallback = new SharedElementCallback() {
        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        @Override
        public void onMapSharedElements(List<String> names, Map<String, View> sharedElements) {
            if (isReturning) {
                ImageView sharedElement = mPagerAdapter.getCurrentFragment().getImage();
                if (sharedElement == null) {
                    names.clear();
                    sharedElements.clear();
                } else if (startPosition != currentPosition) {
                    names.clear();
                    names.add(sharedElement.getTransitionName());
                    sharedElements.clear();
                    sharedElements.put(sharedElement.getTransitionName(), sharedElement);
                }
            }
        }
    };

    private ActivityPhotoListBinding mBinding;
    private PhotoListPagerAdapter mPagerAdapter;

    private ArrayList<String> mPhotos;
    private int startPosition;
    private int currentPosition;
    private boolean isReturning;

    @Override
    protected void initComponent() {
        mPhotos = getIntent().getStringArrayListExtra("photos");
        startPosition = getIntent().getIntExtra("position", 0);

        supportPostponeEnterTransition();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            setEnterSharedElementCallback(mSharedElementCallback);
        }

        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_photo_list);

        mPagerAdapter = new PhotoListPagerAdapter(getSupportFragmentManager(), mPhotos);
        mBinding.vp.setAdapter(mPagerAdapter);
        mBinding.vp.setOffscreenPageLimit(mPhotos.size());
        mBinding.vp.setCurrentItem(startPosition);
    }

    @Override
    protected void createHandler() {
        mBinding.vp.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                currentPosition = position;
            }
        });
    }

    @Override
    protected void loadData(@Nullable Bundle savedInstanceState) {

    }

    @Override
    public void finishAfterTransition() {
        isReturning = true;
        
        super.finishAfterTransition();
    }
}
